const Core = require("@alicloud/pop-core");

module.exports = class Sms {
	static #client = null;
	static #debug = false;

	static init(key, secret, token) {
		this.#client = new Core({
			accessKeyId: key,
			accessKeySecret: secret,
			securityToken: token,
			endpoint: "https://dysmsapi.aliyuncs.com",
			apiVersion: "2017-05-25",
		});
	}

	static debug(isDebug) {
		this.#debug = isDebug;
	}
	static async send(mobile, params, sign, template) {
		if (this.#client == null) {
			throw new Error("未初始化");
		}
		if (this.#debug) {
			console.log("send sms to " + mobile);
		} else {
			let requestParams = {
				PhoneNumbers: mobile,
				SignName: sign,
				TemplateCode: template,
				TemplateParam: JSON.stringify(params),
			};

			let requestOption = {
				method: "POST",
			};

			await this.#client.request("SendSms", requestParams, requestOption);
		}
	}
};
